﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessEntities.Model;
using System.Net.Mail;
using System.Runtime.Serialization;

namespace BusinessEntities
{
    public static class ModelValidation
    {
        public static ModelValidationReport<User> ValidateUser(User user)
        {
            var report = new ModelValidationReport<User>(user);
            report.Valid = true;
            if (!ValidateEmail(user.Email))
            {
                report.Message += "\nThe email is not valid.";
                report.Valid = false;
            }
            return report;
        }

        public static ModelValidationReport<Event> ValidateEvent(Event aEvent)
        {
            return new ModelValidationReport<Event>(aEvent);
        }

        public static ModelValidationReport<Guest> ValidateGuest(Guest guest)
        {
            return new ModelValidationReport<Guest>(guest);
        }

        public static ModelValidationReport<Invite> ValidateInvite(Invite invite)
        {
            if (invite.AdditionalInvitedGuests < 0)
            {
                return new ModelValidationReport<Invite>(invite, false, "Invalid Additional Invited guests.");
            }
            return new ModelValidationReport<Invite>(invite);
        }

        public static ModelValidationReport<Gift> ValidateGuest(Gift gift)
        {
            return new ModelValidationReport<Gift>(gift);
        }

        public static bool ValidateEmail(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
                return false;
            try
            {
                MailAddress addr = new MailAddress(email);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
    [DataContract]
    public class ModelValidationReport<T>
    {
        [DataMember]
        public string Message { get; set; }
        [DataMember]
        public bool Valid { get; set; }
        [DataMember]
        public T ModelObject { get; set; }
        public ModelValidationReport(T modelObject, bool valid = true, string message = "")
        {
            Message = message;
            ModelObject = modelObject;
            Valid = valid;
        }
    }
}
